package com.weixing.mall.provider.controller.front;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.provider.model.domain.LeaveMessage;
import com.weixing.mall.provider.model.dto.LeaveMessageDto;
import com.weixing.mall.provider.model.vo.LeaveMessagePersonVo;
import com.weixing.mall.provider.model.vo.LeaveMessageVo;
import com.weixing.mall.provider.service.ILeaveMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 留言接口
 *
 * @author  wx
 * @date  2019/11/15 17:18
 * @since  1.0
 */
@RestController
@RequestMapping("front/leaveMessage")
public class FrontLeaveMessageController extends BaseController {
    @Autowired
    private ILeaveMessageService leaveMessageService;

    /**
     * 查询个人留言
     *
     * @return 留言
     */
    @GetMapping("")
    public Result<IPage<LeaveMessagePersonVo>> selectByMemberId() {
        MemAuth memAuth = AuthUtil.getMemberExt();
        return ResultUtil.success(leaveMessageService.selectByMemberId(initMpPage(), memAuth.getMid()));
    }

    /**
     * 新增留言
     * @param leaveMessageDto  参数
     * @return 结果
     */
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@Validated @RequestBody LeaveMessageDto leaveMessageDto) {
        MemAuth memAuth = AuthUtil.getMemberExt();
        LeaveMessage leaveMessage = new LeaveMessage();
        BeanUtils.copyProperties(leaveMessageDto, leaveMessage);
        leaveMessage.setMemberId(memAuth.getMid());
        if (null == leaveMessageDto.getId()) {
            leaveMessageService.save(leaveMessage);
        } else {
            leaveMessageService.updateById(leaveMessage);
        }
        return ResultUtil.success();
    }

    /**
     * 查询个人留言详情
     * @param id  留言id
     * @return 详情
     */
    @GetMapping("/{id}")
    public Result<LeaveMessageVo> selectPersonDetail(@PathVariable Long id) {
        LeaveMessage leaveMessage = leaveMessageService.getById(id);
        LeaveMessageVo vo = new LeaveMessageVo();
        BeanUtils.copyProperties(leaveMessage,vo);
        return ResultUtil.success(vo);
    }
}
